package cn.rui.decoration.controller;

import cn.rui.decoration.mapper.CasesMapper;
import cn.rui.decoration.pojo.dto.CaseDTO;
import cn.rui.decoration.pojo.entity.Case;
import cn.rui.decoration.pojo.entity.CaseStyle;
import cn.rui.decoration.pojo.entity.CaseType;
import cn.rui.decoration.pojo.vo.CaseAdminByIdVO;
import cn.rui.decoration.pojo.vo.CaseAdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
public class CasesController {
    @Value("${dirPath}")
    private String dirPath;
    @Autowired(required = false)
    CasesMapper mapper;
    @RequestMapping("/cases/select/admin")
    public List<CaseAdminVO> selectAdmin(){
    return mapper.selectAdmin();
    }
    //风格
    @RequestMapping("/caseType/select")
    public List<CaseType> selectType(){
        return mapper.selectType();

    }
    //户型
    @RequestMapping("/caseStyle/select")
    public List<CaseStyle> selectStyle(){
        return mapper.selectStyle();
    }

    @RequestMapping("/case/insert")
    public void insert(@RequestBody CaseDTO cases){
        Case c = new Case();
        //把DTO里面的属性复制到实体类中
        BeanUtils.copyProperties(cases,c);
        //给属性赋值
        c.setViewCount(0);
        mapper.insert(c);
    }

    @RequestMapping("/csae/selectByIdAdmin")
    public CaseAdminByIdVO selectByIdAdmin(int id){

        return mapper.selectByIdAdmin(id);
    }

    @RequestMapping("/casse/delete")
    public void  deleteCasse(int id){
        //删文件
        CaseAdminByIdVO dcu = mapper.selectByIdAdmin(id);
        new File(dirPath+dcu.getCover()).delete();
        new File(dirPath+dcu.getBedroomUrl()).delete();
        new File(dirPath+dcu.getKitchenUrl()).delete();
        new File(dirPath+dcu.getParlourUrl()).delete();
        new File(dirPath+dcu.getRestaurantUrl()).delete();
        new File(dirPath+dcu.getToiletUrl()).delete();
        mapper.deleteCasse(id);
    }
}
